//
//  TableViewController.m
//  网络记事本
//
//  Created by wukeng on 16/4/2.
//  Copyright © 2016年 wukeng. All rights reserved.
//

#import "TableViewController.h"
#import "AddViewController.h"
#import "NetManager.h"
#import "NoteModel.h"

@interface TableViewController ()

@property (nonatomic,strong) NSArray *list;

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(gotoAdd)];
    self.navigationItem.rightBarButtonItem = right;
    }



-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [NetManager getDataWithSuccessBlock:^(NSArray *arr) {
        self.list = arr;
        //刷新
        [self.tableView reloadData];
    }
                          WithFailBlock:^(NSError *error) {
                              nil;
                          }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void) gotoAdd{
    AddViewController *add = [[AddViewController alloc] init];
    [self showViewController:add sender:nil];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
       return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.list.count ;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    NoteModel *note = [NoteModel noteModelWithDict:self.list[indexPath.row]];
    
    cell.textLabel.text = note.Content;
    
    cell.detailTextLabel.text = note.CDate;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    // 删除
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {
        NSDictionary *dict = self.list[indexPath.row];
        NoteModel *note = [NoteModel noteModelWithDict:dict];
        NSMutableArray *mutArr = [NSMutableArray arrayWithArray:self.list];
        
        // 1. 从数据中删除
        [mutArr removeObjectAtIndex:indexPath.row];
        self.list = mutArr;
        // 2. 表格视图中单元格也要删除
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        //3.网络数据删除
        [NetManager deleteNoteWithNoteID:[note.noteID intValue]];
        
    }
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
